/**
 * ReportGear(2011)
 */
package com.reportgear.designer.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.report.core.api.idname.DefaultIdName;
import com.reportgear.report.core.api.idname.IdName;
import com.reportgear.report.model.SalaryReportModel;

/**
 * 报表列表显示面板,显示保存的报表信息
 * 
 * @version 1.0 2011-4-17
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ReportListView extends JPanel {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -894072785477138732L;
	private JList reports;
	private DefaultListModel reportModel;

	public ReportListView() {
		this.initComponents();
		this.initReports();
	}

	private void initComponents() {
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(200, 20));

		JLabel title = new JLabel("报表列表", JLabel.CENTER);
		title.setPreferredSize(new Dimension(200, 32));
		title.setBorder(BorderFactory.createEtchedBorder());
		this.add(title, BorderLayout.NORTH);

		this.reportModel = new DefaultListModel();
		this.reports = new JList(this.reportModel);
		this.reports.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.reports.setFixedCellHeight(24);
		this.reports.addListSelectionListener(new ListSelectionListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void valueChanged(ListSelectionEvent evt) {
				IdName<Integer> selected = (IdName<Integer>) ((JList) evt.getSource()).getSelectedValue();

				if (!evt.getValueIsAdjusting() && (selected != null)) {
					ReportListView.this.firePropertyChange("currReport", null, selected);
				}
			}
		});

		this.add(new JScrollPane(this.reports), BorderLayout.CENTER);
	}

	/**
	 * 初始化所有报表
	 */
	private void initReports() {
		List<SalaryDataObject> templateList = SalaryHelper.INSTANCE.loadTemplateList();

		for (SalaryDataObject sdo : templateList) {
			this.reportModel.addElement(new DefaultIdName<Integer>(sdo.getInt("id"), sdo.getString("name")));
		}
	}

	/**
	 * 新建报表
	 * 
	 * @param name
	 *            报表名称
	 * @param schemeId
	 *            薪酬模板
	 */
	public void newReport(String name, int schemeId) {
		SalaryReportModel model = new SalaryReportModel();
		model.setVersion("1.0");
		model.setName(name);
		model.setScheme(schemeId);

		int id = SalaryHelper.INSTANCE.saveTemplate(model.toXmlString(), 0, schemeId, name);

		if (id != -1) {
			IdName<Integer> nr = new DefaultIdName<Integer>(id, name);
			this.reportModel.addElement(nr);
			this.reports.setSelectedIndex(this.reportModel.getSize() - 1);
		}
	}

	/**
	 * 删除模板
	 */
	@SuppressWarnings("unchecked")
	public void deleteReport() {
		IdName<Integer> selected = (IdName<Integer>) this.reports.getSelectedValue();

		if (selected != null) {
			if (SalaryHelper.INSTANCE.deleteTemplate(selected.getId())) {
				this.reportModel.removeElement(selected);
				this.selectFirst();
			}
		}
	}

	public void selectFirst() {
		this.reports.setSelectedIndex(0);
	}
}
